import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
} from '@nestjs/common';
import { RoleService } from './role.service';
import { CreateRoleDto } from './dto/create-role.dto';
import { UpdateRoleDto } from './dto/update-role.dto';
import { PageQuery } from '../../../util/entity/page.entity';
import { Role } from './entities/role.entity';
import { HasPerms } from '../../../common/decorator/my.decorator';
import { Perm } from '../../../util/enum/perm.enum';
import { ReqUser } from '../../../common/decorator/user.decorator';

@Controller()
export class RoleController {
  constructor(private readonly roleService: RoleService) {}

  /**
   * 获取ids通过userid
   */

  @HasPerms(Perm.SysUserSetRoles)
  @Get('ids/:userId')
  async getIdsByUserId(@Param('userId') userId: number) {
    return (await this.roleService.getByUserId(userId)).map((role) => role.id);
  }
  /**
   * 设置/提交 角色选择的菜单
   * @param id
   * @param menuIds
   * @param operatorId
   */

  @HasPerms(Perm.SysRoleEdit)
  @Post('menu-select/:id')
  async setRoleSelectedMenus(
    @Param('id') id: number,
    @Body() menuIds: number[],
    @ReqUser('id') operatorId: number,
  ) {
    await this.roleService.dontTouchRoot(id, operatorId);
    return this.roleService.setRoleSelectedMenus(id, menuIds);
  }

  /**
   * 分配权限时候,通过角色id获取角色拥有的菜单ids
   * (但是由于el-tree的特性,需要过过滤掉父节点)
   * @param id
   * @param operatorId
   */

  @HasPerms(Perm.SysRoleSetMenus)
  @Get('menu-ids/:id')
  async getMenuIdsById(
    @Param('id') id: number,
    @ReqUser('id') operatorId: number,
  ) {
    await this.roleService.dontTouchRoot(id, operatorId);
    return this.roleService.getMenuIdsForSelect(id);
  }

  /**
   * 获取角色列表,带分页查询信息
   * @param pageQuery
   */
  @Post('list')
  findAll(@Body() pageQuery: PageQuery<Role>) {
    return this.roleService.findAll(pageQuery);
  }

  /**
   * 创建一个角色
   * @param createRoleDto
   */

  @HasPerms(Perm.SysRoleAdd)
  @Post()
  create(@Body() createRoleDto: CreateRoleDto) {
    return this.roleService.create(createRoleDto);
  }

  /**
   * 查询角色,不带分页(用于用户选择角色下拉框)
   */
  @Get('list')
  findAllNoPage() {
    return this.roleService.findAllNoPage();
  }

  /**
   * 得到角色
   * @param id
   * @param operatorId
   */

  @HasPerms(Perm.SysRoleEdit)
  @Get(':id')
  async findOne(@Param('id') id: number, @ReqUser('id') operatorId: number) {
    await this.roleService.dontTouchRoot(id, operatorId);
    return this.roleService.getOneByOrFail(id);
  }

  /**
   * 修改角色
   * @param id
   * @param updateRoleDto
   * @param operatorId
   */

  @HasPerms(Perm.SysRoleEdit)
  @Patch(':id')
  async update(
    @Param('id') id: number,
    @Body() updateRoleDto: UpdateRoleDto,
    @ReqUser('id') operatorId: number,
  ) {
    await this.roleService.dontTouchRoot(id, operatorId);
    return this.roleService.update(id, updateRoleDto);
  }

  /**
   * 删除角色
   * @param id
   * @param operatorId
   */

  @HasPerms(Perm.SysRoleDelete)
  @Delete(':id')
  async remove(@Param('id') id: number, @ReqUser('id') operatorId: number) {
    await this.roleService.dontTouchRoot(id, operatorId);
    return this.roleService.remove(id);
  }
}
